package file;

import java.io.*;

public class HomeWork02 {
    /**
     * 将当前目录下的所有文件都复制一份，复制的文件命名为:原
     * 文件名_cp.后缀
     * 比如原文件为:test.dat
     * 复制后的文件为:test_cp.dat
     *
     *
     * @author dc
     *
     */
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        File filee = new File("copy");
        filee.mkdir();
        byte[] bs = new byte[1024*10];
        File[] files = file.listFiles(file1 -> file1.isFile());
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            String name = f.getName();
            int index = name.lastIndexOf(".");
            String substring = name.substring(0, index);
            String substring1 = name.substring(index + 1);
            String newName = substring + "_cp." + substring1;
            FileInputStream fis = new FileInputStream(f);
            FileOutputStream fos = new FileOutputStream("copy/"+newName);
            int len;
            while ((len = fis.read(bs)) != -1) {
                fos.write(bs,0,len);
            }
            fis.close();
            fos.close();
        }

    }
}
